<?php

namespace common\service\step_category_month_report;

use common\model\EntityModel;

/**
 * 成片报表
 *  通过率必定是100%
 *  工单问题占比必定是0%
 */
class CompleteFilmReportCount extends EpisodeReportCountBase
{

    protected $planDonePlanData = [];

    public function getPlanDonePlanData($projectId, $monthStart, $monthEnd, $stepCategoryIdList)
    {
        if (!empty($this->planDonePlanData)) {
            return $this->planDonePlanData;
        }
        $this->planDonePlanData = model(EntityModel::class)
            ->where([
                'module_id' => module_by_code('episode')['id'],
                'project_id' => $projectId,
                "json->>'$.episode_plan_seal_time'" => ['BETWEEN', [$monthStart, $monthEnd]],
            ])
            ->field("id,json->>'$.episode_plan_seal_time' episode_plan_seal_time,json->>'$.episode_seal_time' episode_seal_time")
            ->select();
        return $this->planDonePlanData;
    }

    /**
     * 计算统计数据
     * @param $projectId
     * @param $stepCategoryIdList
     * @param $monthStart
     * @param null $monthEnd
     * @return void
     */
    public function calculate($projectId, $stepCategoryIdList, $monthStart, $monthEnd = null)
    {
        $this->countObject = new StepCategoryMonthCountObject();
        $monthStart = strtotime($monthStart);
        if (empty($monthEnd)) {
            $monthEnd = strtotime("+1 month -1 second", $monthStart);
        } else {
            $monthEnd = strtotime($monthEnd);
            $this->countObject->canBeSaved = false;
        }
        $planData = $this->getPlanDonePlanData($projectId, $monthStart, $monthEnd, $stepCategoryIdList);

        $planEpisode = count($planData);
        $this->countObject->deliveryUnit = '集';
        $this->countObject->projectId = $projectId;
        $this->countObject->tenantId = request()->getTenantId();
        $this->countObject->monthTime = $monthStart;
        $this->countObject->stepCategoryIds = implode(',', $stepCategoryIdList);

        $this->countObject->planDelivery = $planEpisode;
        $this->countObject->shouldDelivery = $planEpisode;
        $this->countObject->addDelivery = 0;
        $this->countObject->passPercent = 1;
        $this->countObject->taskWorkOrderPercent = 0;
        $this->countObject->havePlan = !empty($planData);
        if ($planEpisode > 0) {
            $episodeIdList = array_column($planData, 'id');
            $episodeStepCategorySealTimeList = model(EntityModel::class)
                ->where([
                    [
                        'id' => ['IN', implode(',', $episodeIdList)],
                        "json->>'$.episode_seal_time'" => ['BETWEEN', [$monthStart, $monthEnd]],
                        '_logic' => 'OR',
                    ]
                ])
                ->where(["project_id" => $projectId])
                ->field("id,json->>'$.episode_plan_seal_time' episode_plan_seal_time,json->>'$.episode_seal_time' episode_seal_time")
                ->select();

            foreach ($episodeStepCategorySealTimeList as $episodeStepCategorySealTimeItem) {
                if ($episodeStepCategorySealTimeItem['episode_seal_time'] <= $monthEnd && $episodeStepCategorySealTimeItem['episode_seal_time'] >= $monthStart) {
                    //  已经交付
                    $this->countObject->delivery += 1;
                }
                // 异常交付
                if (
                    in_array($episodeStepCategorySealTimeItem['id'], $episodeIdList)
                    && (
                        (
                            $episodeStepCategorySealTimeItem['episode_seal_time'] > 0
                            && $episodeStepCategorySealTimeItem['episode_seal_time'] > $episodeStepCategorySealTimeItem['episode_plan_seal_time']
                        )
                        || (
                            $episodeStepCategorySealTimeItem['episode_seal_time'] <= 0
                            && $episodeStepCategorySealTimeItem['episode_plan_seal_time'] < time()
                        )
                    )
                ) {
                    $this->countObject->abnormal += 1;
                }
            }
        }
        // 交付率
        $this->setDeliveryPercent();
        $this->setIsPlanDone();
        $this->saveCountObjectData();
    }
}
